//
//  GameEngine.m
//  BabyTour
//
//  Created by Wander See on 11-2-13.
//  Copyright 2011 Tongji Apple Club. All rights reserved.
//

#import "GameEngine.h"
#import "CBArena.h"
#import "CBArenaLayer.h"

@implementation GameEngine


+ (void)startCocos {
    // Prepare cocos3d.
    //CC3Layer* cc3Layer = [CC3DemoMashUpLayer layerWithColor: ccc4(100, 120, 220, 255)];
    //CC3World* cc3World = [CC3DemoMashUpWorld world];
    CC3Layer* cc3Layer = [CBArenaLayer layerWithColor: ccc4(100, 120, 220, 255)];
    CC3World* cc3World = [CBArena world];
    cc3Layer.cc3World = cc3World;
    [cc3World play];
    [cc3Layer scheduleUpdate];
    
	CCScene *scene = [CCScene node];
	[scene addChild: cc3Layer];
    if ([[CCDirector sharedDirector] runningScene]) {
        [[CCDirector sharedDirector] replaceScene:scene];
        [[CCDirector sharedDirector] resume];
    } else {
        [[CCDirector sharedDirector] runWithScene:scene];
    }
}

+ (CC3World *)currentWorld {
    if (![[CCDirector sharedDirector] runningScene]) {
        return nil;
    }
    
    CC3Layer *currentLayer = (CC3Layer *)[[[[CCDirector sharedDirector] runningScene] children] objectAtIndex:0];
    return currentLayer.cc3World;
}

+ (EAGLView *)getEngineView {
    return [GameEngine getEngineView:CGRectMake(0, 0, 1024, 768)];
}

+ (EAGLView *)getEngineView:(CGRect)frame {
    if(![CCDirector setDirectorType:kCCDirectorTypeDisplayLink] )
		[CCDirector setDirectorType:kCCDirectorTypeDefault];
	
	
	CCDirector *director = [CCDirector sharedDirector];
	[director setDeviceOrientation:kCCDeviceOrientationPortrait];
	[director setAnimationInterval:1.0/60];
	[director setDisplayFPS:YES];

    // EAGLView.
	EAGLView *glView = [EAGLView viewWithFrame:frame
								   pixelFormat:kEAGLColorFormatRGBA8
								   depthFormat:GL_DEPTH_COMPONENT16_OES];
	[glView setMultipleTouchEnabled: YES];
    [director setOpenGLView:glView];
	return glView;
}


+ (SEL)selectorForToolState:(NSString *)toolState {
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"stateSelectors" ofType:@"plist"];
    NSDictionary *aDict = [NSDictionary dictionaryWithContentsOfFile:filePath];
    NSString *selName = [aDict objectForKey:toolState];
    //NSLog(@"The sel is %@.", selName);
    return NSSelectorFromString(selName);
}


+ (NSString *)effectForToolState:(NSString *)toolState {
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"ToolEffects" ofType:@"plist"];
    NSDictionary *aDict = [NSDictionary dictionaryWithContentsOfFile:filePath];
    NSString *theEffectName = [aDict objectForKey:toolState];
    return theEffectName;
}



@end

